package com.qf.ayc.foster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.ayc.foster.entity.Foster;
import com.qf.ayc.foster.mapper.FosterMapper;
import com.qf.ayc.foster.service.IFosterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.ayc.foster.vo.FosterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author BeautyFairy
 * @since 2024-02-20
 */
@Service
@Slf4j
public class FosterServiceImpl extends ServiceImpl<FosterMapper, Foster> implements IFosterService {
    @Autowired
    private FosterMapper fosterMapper;

    @Override
    public Boolean add(Foster foster) {
        log.info("接受到的寄养={}", foster);
        foster.setId(null);
        int insert = fosterMapper.insert(foster);
        System.out.println(insert);
        return true;
    }

    @Override
    public FosterVo getlist(Foster foster, Integer pageNum, Integer limit) {
//        String username = foster.getUsername();
//        String petname = foster.getPetname();
//        String type = foster.getType();
//        String phone = foster.getPhone();
//        Date endtime = foster.getEndtime();
//        Date starttime = foster.getStarttime();
//        Integer status = foster.getStatus();
//        log.info("username:{},petname:{},type:{},phone:{},endtime:{},starttime:{}",username,petname,type,phone,endtime,starttime);
        //好几个条件去数据库里面查询
        QueryWrapper<Foster> wrapper = new QueryWrapper<>();
        QueryWrapper<Foster> eqs = wrapper
                .eq(foster.getUsername() != null&&foster.getUsername()!="", "username", foster.getUsername())
                .eq(foster.getPetname()!= null&&foster.getPetname()!="", "petname", foster.getPetname())
                .eq(foster.getType()!=null&&foster.getType()!="","type",foster.getType())
                .eq(foster.getPhone() != null&&foster.getPhone()!="", "phone", foster.getPhone())
                .eq(foster.getEndtime() != null, "endtime", foster.getEndtime())
                .eq(foster.getStatus()!=null,"status",foster.getStatus())
                .eq(foster.getStarttime() != null, "starttime",foster.getStarttime());
        Page<Foster> page = new Page<>(pageNum,limit);
        fosterMapper.selectPage(page, wrapper);
        List<Foster> records = page.getRecords();
        Long count = fosterMapper.selectCount(wrapper);
        log.info("分页所有的商品信息为={}", records);
        return new FosterVo(records,count);
    }

    //这里前端不能改id
    @Override
    public Boolean updatefoster(Foster foster) {
        log.info("foster:{}",foster);
        int i = fosterMapper.updateById(foster);
        log.info("后端返回修改值={}", i);
        if (i != 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean del(String id) {
        int i = fosterMapper.deleteById(id);
        log.info("寄养信息删除返回值={}", i);
        if (i != 0) {
            return true;
        } else {
            return false;
        }

    }

//    @Override
//    public List<Foster> find(Foster foster) {
//        String username = foster.getUsername();
//        String petname = foster.getPetname();
//        String type = foster.getType();
//        String phone = foster.getPhone();
//        Date endtime = foster.getEndtime();
//        Date starttime = foster.getStarttime();
//        Integer status = foster.getStatus();
//        log.info("username:{},petname:{},type:{},phone:{},endtime:{},starttime:{}",username,petname,type,phone,endtime,starttime);
//        //好几个条件去数据库里面查询
//        QueryWrapper<Foster> wrapper = new QueryWrapper<>();
//        QueryWrapper<Foster> eqs = wrapper
//                .eq(username != null&&username!="", "username", username)
//                .eq(petname != null&&petname!="", "petname", petname)
//                .eq(type!=null&&type!="","type",type)
//                .eq(phone != null&&phone!="", "phone", phone)
//                .eq(endtime != null, "endtime", endtime)
//                .eq(status!=null,"status",status)
//                .eq(starttime != null, "starttime", starttime);
//        log.info("eqs={}" ,eqs);
//        List<Foster> fosters = fosterMapper.selectList(eqs);
//        log.info("{}",fosters);
//        return fosters;
//    }
}
